using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;
using System.Data.Common;
using System.Text;

namespace JNPF.Basics.Logic
{
    public class PointsdetailsService : DataFactory, PointsdetailsIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<PointsdetailsEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<PointsdetailsEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<PointsdetailsListOutput>> GetPageList(PointsdetailsListQueryInput input)
        {
            input.sidx = "F_CreatorTime";
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	                            poin.F_ID,
	                            poin.F_UserId,
	                            poin.F_State,
	                            poin.F_Type,
	                            poin.F_CustomerLoanUUID,
	                            poin.F_ChildUserId,
	                            poin.F_PublicName,
	                            poin.F_Points,
	                            poin.F_PointsAfter,
	                            poin.F_PointsFront,
	                            poin.F_SortCode,
	                            poin.F_EnabledMark,
	                            poin.F_CreatorTime,
	                            poin.F_CreatorUser,
	                            poin.F_CreatorUserId,
	                            poin.F_LastModifyTime,
	                            poin.F_LastModifyUserId,
	                            poin.F_DeleteMark,
	                            poin.F_DeleteTime,
	                            poin.F_DeleteUserId,
	                            u.F_REALNAME userName 
                            FROM
	                            jiushi_Pointsdetails poin
	                            LEFT JOIN base_user u ON poin.F_UserId = u.F_ID 
                            WHERE
	                            1 =1");

                //查询手机号
                if (!string.IsNullOrEmpty(input.userName))
                {

                    dbSql.Append(" AND   u.F_REALNAME like  @userName");
                    parameters.Add(DataHelper.CreateParameter("@userName", "%" + input.userName + "%"));
                }

                if (!input.isAdmin)
                {
                    UserInfo userInfo = UserProvider.Instance.Get();
                    //自己只能看到自己的
                    dbSql.Append(" and poin.F_USERID =  @userId");
                    parameters.Add(DataHelper.CreateParameter("@userId", userInfo.UserId));
                }

                return await PageHelper.GetPageResultAsync<PointsdetailsListOutput>(db, input, dbSql.ToString(), parameters.ToArray());

            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(PointsdetailsEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(PointsdetailsEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(PointsdetailsEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<PointsdetailsEntity>> GetList(PointsdetailsListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<PointsdetailsEntity>();
                //查询用户主键
                if (!string.IsNullOrEmpty(input.userId))
                {
                    queryWhere = queryWhere.And(m => m.UserId.Contains(input.userId));
                }
                //查询类别：1：获得 -1：消耗
                if (input.state.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.State == input.state);
                }
                //查询类别：1.本人推荐、2.裂变获得,-1:消耗
                if (input.type.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Type == input.type);
                }
                //查询客户ID
                if (!string.IsNullOrEmpty(input.customerId))
                {
                    //queryWhere = queryWhere.And(m => m.CustomerId.Contains(input.customerId));
                }
                //查询合伙人用户Id
                if (!string.IsNullOrEmpty(input.partnerUserId))
                {
                    //queryWhere = queryWhere.And(m => m.PartnerUserId.Contains(input.partnerUserId));
                }
                //查询积分
                if (input.pointsFront.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.PointsFront == input.pointsFront);
                }
                //查询积分
                if (input.points.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Points == input.points);
                }
                //查询积分
                if (input.pointsAfter.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.PointsAfter == input.pointsAfter);
                }


                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<PointsdetailsEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}